package cn.minds.capture.dispatch.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import cn.minds.capture.dispatch.CaputureInputStream;
import cn.minds.capture.dispatch.CaputureOutputStream;
import cn.minds.capture.dispatch.Connect;
import cn.minds.capture.dispatch.TaskManager;

public class HandlerServer extends Connect {

	public HandlerServer(TaskManager taskManager, Socket socket) {
		setSocket(socket);
		setTaskManager(taskManager);
		taskManager.setConnect(this);
	}

	public void run() {
		connect();
	}

	public boolean connect() {
		Socket socket = getSocket();
		System.out.println(socket.getInetAddress().toString() + " connect");
		try {
			InputStream in = socket.getInputStream();
			OutputStream out = socket.getOutputStream();
			setInputStream(new CaputureInputStream(in));
			setOutputStream(new CaputureOutputStream(out));
			readCommand();
			return true;
		} catch (IOException e) {
			disConnect();
			return false;
		}
	}

}
